/*
 ============================================================================
 Name        : Set_CGI.c
 Author      : liuzhanchun
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================
 */

#if 1
#define SERVER_NAME cgiServerName
#endif
#if 0
#define SERVER_NAME "www.boutell.com"
#endif

/* You may need to change this, particularly under Windows;
	it is a reasonable guess as to an acceptable place to
	store a saved environment in order to test that feature.
	If that feature is not important to you, you needn't
	concern yourself with this. */

#ifdef WIN32
#define SAVED_ENVIRONMENT "c:\\cgicsave.env"
#else
#define SAVED_ENVIRONMENT "/tmp/cgicsave.env"
#endif /* WIN32 */

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include "config_ini.h"

int cgiMain() {
    /*从html获取数据，设置cookie*/
        //local ip
        char localIp1[8] = {0};
	cgiFormString("localIp1", localIp1, 8);

	char localIp2[8] = {0};
	cgiFormString("localIp2", localIp2, 8);

	char localIp3[8] = {0};
	cgiFormString("localIp3", localIp3, 8);

	char localIp4[8] = {0};
	cgiFormString("localIp4", localIp4, 8);

	char local_ip[32] = {0};
	sprintf(local_ip,"%s.%s.%s.%s", localIp1, localIp2, localIp3, localIp4);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","local_ip",local_ip);

	//local mask
	char localMask1[8] = {0};
	cgiFormString("localMask1", localMask1, 8);

	char localMask2[8] = {0};
	cgiFormString("localMask2", localMask2, 8);

	char localMask3[8] = {0};
	cgiFormString("localMask3", localMask3, 8);

	char localMask4[8] = {0};
	cgiFormString("localMask4", localMask4, 8);

	char local_mask[32] = {0};
	sprintf(local_mask,"%s.%s.%s.%s", localMask1, localMask2, localMask3, localMask4);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","local_mask",local_mask);

  //local gateway1
  char localGateway1[8] = {0};
  cgiFormString("localGateway1", localGateway1, 8);

	char localGateway2[8] = {0};
	cgiFormString("localGateway2", localGateway2, 8);

	char localGateway3[8] = {0};
	cgiFormString("localGateway3", localGateway3, 8);

	char localGateway4[8] = {0};
	cgiFormString("localGateway4", localGateway4, 8);

	char local_gateway[32] = {0};
  sprintf(local_gateway,"%s.%s.%s.%s", localGateway1, localGateway2, localGateway3, localGateway4);
	fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","local_gateway",local_gateway);

  //remote ip
  char remoteIp1[8] = {0};
  cgiFormString("remoteIp1", remoteIp1, 8);

  char remoteIp2[8] = {0};
  cgiFormString("remoteIp2", remoteIp2, 8);

  char remoteIp3[8] = {0};
  cgiFormString("remoteIp3", remoteIp3, 8);

  char remoteIp4[8] = {0};
  cgiFormString("remoteIp4", remoteIp4, 8);

  char remote_ip[32] = {0};
  sprintf(remote_ip,"%s.%s.%s.%s", remoteIp1, remoteIp2, remoteIp3, remoteIp4);
  fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","remote_ip",remote_ip);

	char remote_port[32] = {0};
	cgiFormString("remote_port", remote_port, 32);
        fprintf(cgiOut,"Set-Cookie: %s=%s;  path=/\r\n","remote_port",remote_port);

	/* Send the content type, letting the browser know this is HTML */
	cgiHeaderContentType("text/html; charset=UTF-8");

	/* Top of the page */
	fprintf(cgiOut, "<HTML><HEAD>\n");
	fprintf(cgiOut, "<TITLE>web 管理</TITLE></HEAD>\n");
	fprintf(cgiOut, "<BODY><H1></H1>\n");

	//存储数据
	configFileStrSet("/boa/my.ini","local_ip",local_ip);
	configFileStrSet("/boa/my.ini","local_gateway",local_gateway);
	configFileStrSet("/boa/my.ini","local_mask",local_mask);
	configFileStrSet("/boa/my.ini","remote_ip",remote_ip);
	configFileStrSet("/boa/my.ini","remote_port",remote_port);

	//刷新页面
	fprintf(cgiOut, "<meta http-equiv=Refresh content=0;URL=../manager.html\n");
	fprintf(cgiOut, "</BODY></HTML>\n");

	return 0;
}
